package com.zhongying.customer;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.rpc.Result;
import com.alibaba.fastjson.JSONObject;
import com.haokang.jf.service.CouponSendService;
import com.zhongying.jf.ResConstants;
import com.zhongying.jf.service.OrderInvestService;
import com.zhongying.jf.service.UserAccountPageService;
import com.zhongying.jf.util.ArrayIsNotNullUtil;
import com.zhongying.sys.utils.PageUtils;
import com.zhongying.sys.utils.R;

/**
 * @author 周 浩
 * @email zhou_eric90@163.com
 * @date 2018年4月3日 下午1:47:25
 * @描述 红包记录
 */
@SuppressWarnings("unchecked")
@Controller
@RequestMapping("/coupon")
public class CouponController {

	@Autowired
	CouponSendService couponSendService;

	@Autowired
	OrderInvestService orderInvestService;

	@Autowired
	private UserAccountPageService userAccountPageService;

	/**
	 * 
	 * @param startTime
	 * @param endTime
	 * @param tradeType
	 * @param userCode
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/list_records")
	@ResponseBody
	public R listCouponRecords(String startTime, String endTime, String tradeType, String userCode, Integer page,
			Integer limit) {

		if (userCode == null || page == null || page <= 0 || limit == null || limit <= 0) {
			return R.error(-1, "查询参数不合法");
		}
		Map<String, Object> inMap = new HashMap<>();
		inMap.put("create_start_time", startTime);
		inMap.put("create_end_time", endTime);
		inMap.put("tradeType", "tradeType");// 交易类型，待定
		inMap.put("userCode", userCode);
		int offset = (page - 1) * limit;
		inMap.put("offset", offset);
		inMap.put("limit", limit);
		Map<String, Object> resultMap = couponSendService.queryUserCoupon(inMap);
		int total = (int) resultMap.get("count");
		List<Map<String, Object>> result = (List<Map<String, Object>>) resultMap.get("data");
		List<Map<String, Object>> list = new ArrayList<>();
		if (result != null) {
			for (Map<String, Object> map : result) {
				Map<String, Object> data = new HashMap<>();
				data.put("createTime", map.get("createTime"));
				String couponType = map.get("type").toString();
				switch (couponType) {
				case "1":
					data.put("type", "加息券");
					break;
				case "2":
					data.put("type", "抵扣卷");
					break;
				case "3":
					data.put("type", "返现券");
					break;
				default:
					break;
				}
				BigDecimal coupon_amount = (BigDecimal) map.get("couponAmount");
				if (couponType.equals("1")) {
					data.put("coupon_amount", coupon_amount.intValue() + "%");
				} else {
					data.put("coupon_amount", coupon_amount.intValue() + "元");
				}

				String minAmount = map.get("minAmount").toString();
				String maxAmount = map.get("maxAmount").toString();

				StringBuilder investScope = new StringBuilder();
				investScope.append("[" + minAmount + ",");
				if ("0".equals(maxAmount)) {
					investScope.append("∞");
				} else {
					investScope.append(maxAmount);
				}
				investScope.append("]");
				data.put("investScope", investScope.toString());
				StringBuilder dayScope = new StringBuilder();
				String minDays = map.get("minDays").toString();
				String maxDays = map.get("maxDays").toString();
				dayScope.append("[" + minDays + ",");
				if ("0".equals(maxDays)) {
					dayScope.append("∞");
				} else {
					dayScope.append(maxDays);
				}
				dayScope.append("]");
				data.put("dayScope", dayScope.toString());

				StringBuilder periodOfValidity = new StringBuilder();

				String startDate = map.get("startDate").toString();
				String endDate = map.get("endDate").toString();
				periodOfValidity.append(startDate + "-" + endDate);
				data.put("periodOfValidity", periodOfValidity.toString());
				String status = map.get("status").toString();
				switch (status) {
				case "1":
					data.put("status", "未使用(已领取）");
					break;
				case "2":
					data.put("status", "已使用");
					break;
				case "3":
					data.put("status", "已过期");
					break;
				case "4":
					data.put("status", "冻结");
					break;
				default:
					break;
				}
				data.put("recordCode", map.get("recordCode"));
				list.add(data);
			}
		}

		PageUtils pageUtil = new PageUtils(list, total, limit, page);
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 查看红包使用标的详情
	 * 
	 * @param couponId
	 *            红包ID
	 * @return
	 */
	@RequestMapping("/show_borrow_detail")
	@ResponseBody
	public R showCouponBorrowDetail(String couponId) {
		if (couponId == null || couponId.equals("")) {
			return R.error(-1, "查询参数不合法");
		}
		String orderNo = orderInvestService.selectOrderNoByCouponNo(couponId);
		if (orderNo == null || orderNo.equals("")) {
			return R.error(-1, "红包：" + couponId + "没有使用记录");
		}
		Map inMap = new HashMap<>();
		inMap.put("orderNo", orderNo);
		ArrayIsNotNullUtil.paramsValidate(inMap);
		com.zhongying.jf.result.Result result = userAccountPageService.userProductDetail(inMap);
		if (ResConstants.SUCCESS.getCode().equals(result.getCode())) {
			List<Object> list = new ArrayList<>();
			list.add(result.getModel());
			PageUtils pageUtil = new PageUtils(list, 1, Integer.MAX_VALUE, 1);
			return R.ok().put("page", pageUtil);
		} else {
			return R.error(-1, result.getMsg());
		}
	}

	/**
	 * 查看红包使用标的详情
	 * 
	 * @param couponId
	 *            红包ID
	 * @return
	 */
	@RequestMapping("/show_invest_detail")
	@ResponseBody
	public R showCouponInvestDetail(String couponId) {
		if (couponId == null || couponId.equals("")) {
			return R.error(-1, "查询参数不合法");
		}
		String orderNo = orderInvestService.selectOrderNoByCouponNo(couponId);
		if (orderNo == null || orderNo.equals("")) {
			return R.error(-1, "红包：" + couponId + "没有使用记录");
		}
		Map inMap = new HashMap();
		inMap.put("orderNo", orderNo);
		ArrayIsNotNullUtil.paramsValidate(inMap);
		com.zhongying.jf.result.Result result = userAccountPageService.userBillInvestDetail(inMap);
		if (ResConstants.SUCCESS.getCode().equals(result.getCode())) {
			List<?> list = (List) result.getModel();
			PageUtils pageUtil = new PageUtils(list, list.size(), Integer.MAX_VALUE, 1);
			return R.ok().put("page", pageUtil);
		} else {
			return R.error(-1, result.getMsg());
		}
	}
}
